package com.ssm.zy.service.impl;

import com.ssm.zy.mapper.CommentMapper;
import com.ssm.zy.mapper.FollowMapper;
import com.ssm.zy.mapper.LabelMapper;
import com.ssm.zy.mapper.PostMapper;
import com.ssm.zy.pojo.Label;
import com.ssm.zy.pojo.Post;
import com.ssm.zy.pojo.vo.CommentVo;
import com.ssm.zy.pojo.vo.PostVo;
import com.ssm.zy.service.PostService;
import com.ssm.zy.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private FollowMapper followMapper;

    public List<PostVo> check(Integer sort, Integer userId, Integer labelId, String likeName) {
        List<PostVo> postVoList = postMapper.check(sort,labelId,likeName);
        return setPostVoList(postVoList,userId);
    }

    public List<PostVo> getcheckUserPost(Integer userId) {
        List<PostVo> postVoList = postMapper.getcheckUserPost(userId);
        return setPostVoList(postVoList,userId);
    }

    public List<PostVo> getcheckUserCollectPost(Integer userId) {
        List<PostVo> postVoList = postMapper.getcheckUserCollectPost(userId);
        return setPostVoList(postVoList,userId);
    }

    public List<PostVo> getcheckUserPraisePost(Integer userId) {
        List<PostVo> postVoList = postMapper.getcheckUserPraisePost(userId);
        return setPostVoList(postVoList,userId);
    }

    @Transactional
    public List<CommentVo> commentDetails(Integer postId, Integer userId) {
        List<CommentVo> commentVoList = commentMapper.commentDetails(postId);
        for (CommentVo commentVo : commentVoList) {
            //判断是否点赞
            commentVo.setIsPraise(postMapper.getPraiseOfUserComment(
                    commentVo.getCommentId()).contains(userId));
            List<CommentVo> commentVos=commentMapper.selectComment(commentVo.getCommentId());
            for (CommentVo vo : commentVos) {
                vo.setIsPraise(postMapper.getPraiseOfUserComment(
                        vo.getCommentId()).contains(userId));
            }
            commentVo.setCommentVoList(commentVos);
        }
        postMapper.addPostView(postId);//浏览量+1
        return commentVoList;
    }

    public int commentPost(Integer postId, Integer userId, String commentContent) {
        Date date = new Date();
        return commentMapper.addCommentPost(postId,userId,commentContent,date);
    }

    @Override
    public List<Label> getLabel() {
        return labelMapper.getLabel();
    }

    @Transactional
    public int uploadPost(PostVo postVo, Integer userId){
        Date date = new Date();
        if(postVo.getPostPhoto()==null || postVo.getPostPhoto().isEmpty()){
            postVo.setPostPhoto("https://pic2.zhimg.com/v2-f2baf85af5f74f1371afbd41f8f42651_r.jpg");
        }
        postVo.setPostTime(date);
        postVo.setUserId(userId);
        int i=postMapper.addPost(postVo);
        List<Integer> labelList=new ArrayList<>();
        List<Label> labelLists = postVo.getLabelList();
        for (Label list : labelLists) {
            labelList.add(list.getLabelId());
        }
        int j=postMapper.addPostLabel(postVo.getPostId(),labelList);
        if(i>0&&j>0){
            return i+j;
        }
        i=1/0;
        return 0;
    }

    private List<PostVo> setPostVoList(List<PostVo> postVoList,Integer userId) {
        for (PostVo postVo : postVoList) {
            //判断是否关注
            postVo.setIsFollow(followMapper.getPassiveFocusId(
                    postVo.getUserId()).contains(userId) || userId == postVo.getUserId());
            //判断是否点赞
            postVo.setIsPraise(postMapper.getPraiseOfUser(
                    postVo.getPostId()).contains(userId));
            //判断是否收藏
            postVo.setIsCollect(postMapper.getCollectOfUser(
                    postVo.getPostId()).contains(userId));
            //获取标签
            postVo.setLabelList(labelMapper.getLabelById(postVo.getPostId()));
        }
        return postVoList;
    }

    @Override
    public int reportPost(Integer postId) {
        return postMapper.reportPost(postId);
    }

    @Transactional
    @Override
    public int addpraisePost(Integer userId, Integer postId) {
        List<Integer> userIdList=postMapper.getPraiseOfUser(postId);
        int i=0;
        int j=0;
        if(userIdList.contains(userId)){//包含说明已经点过赞，需要取消点赞
            i=postMapper.delpraisePost(userId,postId);
            j=postMapper.updateDelPostpraise(postId);
        }else{//不包含说明没有点过
            i=postMapper.addpraisePost(userId,postId);
            j=postMapper.updateAddPostpraise(postId);
        }
        if(i>0 && j>0){
            return i+j;
        }
        i=1/0;
        return 0;
    }

    @Transactional
    @Override
    public int collectPost(Integer userId, Integer postId) {
        List<Integer> userIdList=postMapper.getCollectOfUser(postId);
        int i=0;
        int j=0;
        if(userIdList.contains(userId)){//包含说明已经收藏，需要取消
            i=postMapper.delpostCollect(userId,postId);
            j=postMapper.updateDelpostCollect(postId);
        }else{//不包含说明没有收藏
            i=postMapper.addpostCollect(userId,postId);
            j=postMapper.updateAddpostCollect(postId);
        }
        if(i>0 && j>0){
            return i+j;
        }
        i=1/0;
        return 0;
    }

    @Transactional
    @Override
    public int updateUserPost(PostVo postVo){
        int i = postMapper.updateUserPost(postVo);
        int j = postMapper.delUserPostLabel(postVo.getPostId());
        List<Integer> labelList=new ArrayList<>();
        List<Label> labelLists = postVo.getLabelList();
        for (Label list : labelLists) {
            labelList.add(list.getLabelId());
        }
        int k = postMapper.addPostLabel(postVo.getPostId(),labelList);
        if(i>0&&k>0){
            return i+j+k;
        }
        i=1/0;
        return 0;
    }

    @Override
    public List<PostVo> viewerCheck(Integer sort, Integer labelId, String likeName) {
        return postMapper.check(sort,labelId,likeName);
    }

    @Override
    public List<PostVo> toCheckUserPost(Integer userId) {
        List<PostVo> postVoList = postMapper.toCheckUserPost(userId);
        return setPostVoList(postVoList,userId);
    }
}
